{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 断点续训，存取模型\n",
    "\n",
    "###### 读取保存模型：\n",
    "\n",
    "###### 读取模型：\n",
    "    load_weights(路径文件名）\n",
    "eg\n",
    "\n",
    "    checkpoint_save_path = \"./checkpoint/fashion.ckpt\"\n",
    "    if os.path.exists(checkpoint_save_path + '.index'):#在生成ckpt时会同时生成索引\n",
    "        print('-------------load the model-----------------')\n",
    "        model.load_weights(checkpoint_save_path)\n",
    "\n",
    "###### 保存模型：\n",
    "    tf.keras.callbacks.ModelCheckpoint(\n",
    "    filepath=路径文件名,\n",
    "    save_weights_only=True/False,\n",
    "    save_best_only=True/False)\n",
    "    history = model.fit（ callbacks=[cp_callback] ）\n",
    "eg    \n",
    "\n",
    "    cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,\n",
    "                                     save_weights_only=True,#只保留模型参数\n",
    "                                     save_best_only=True)#只保留最优模型\n",
    "    history = model.fit(x_train, y_train, batch_size=32, epochs=5, \n",
    "                 validation_data=(x_test, y_test), \n",
    "                 validation_freq=1,\n",
    "                 callbacks=[cp_callback])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------load the model-----------------\n",
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/5\n",
      "60000/60000 [==============================] - 19s 316us/sample - loss: 0.2944 - sparse_categorical_accuracy: 0.8915 - val_loss: 0.3481 - val_sparse_categorical_accuracy: 0.8774\n",
      "Epoch 2/5\n",
      "60000/60000 [==============================] - 19s 309us/sample - loss: 0.2785 - sparse_categorical_accuracy: 0.8974 - val_loss: 0.3469 - val_sparse_categorical_accuracy: 0.8751\n",
      "Epoch 3/5\n",
      "60000/60000 [==============================] - 24s 407us/sample - loss: 0.2670 - sparse_categorical_accuracy: 0.9014 - val_loss: 0.3365 - val_sparse_categorical_accuracy: 0.8816\n",
      "Epoch 4/5\n",
      "60000/60000 [==============================] - 21s 350us/sample - loss: 0.2560 - sparse_categorical_accuracy: 0.9044 - val_loss: 0.3330 - val_sparse_categorical_accuracy: 0.8812\n",
      "Epoch 5/5\n",
      "60000/60000 [==============================] - 22s 368us/sample - loss: 0.2462 - sparse_categorical_accuracy: 0.9080 - val_loss: 0.3395 - val_sparse_categorical_accuracy: 0.8804\n",
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten_1 (Flatten)          multiple                  0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              multiple                  100480    \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              multiple                  1290      \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "\n",
    "fashion = tf.keras.datasets.fashion_mnist\n",
    "(x_train, y_train), (x_test, y_test) = fashion.load_data()\n",
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
    "\n",
    "model = tf.keras.models.Sequential([\n",
    "    tf.keras.layers.Flatten(),\n",
    "    tf.keras.layers.Dense(128, activation='relu'),\n",
    "    tf.keras.layers.Dense(10, activation='softmax')\n",
    "])\n",
    "\n",
    "model.compile(optimizer='adam',\n",
    "              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n",
    "              metrics=['sparse_categorical_accuracy'])\n",
    "\n",
    "checkpoint_save_path = \"./checkpoint/fashion.ckpt\"\n",
    "if os.path.exists(checkpoint_save_path + '.index'):\n",
    "    print('-------------load the model-----------------')\n",
    "    model.load_weights(checkpoint_save_path)\n",
    "\n",
    "cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,\n",
    "                                                 save_weights_only=True,\n",
    "                                                 save_best_only=True)\n",
    "\n",
    "history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,\n",
    "                    callbacks=[cp_callback])\n",
    "model.summary()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
